package com.imooc.controller;

import com.imooc.bo.Base64FileBO;
import com.imooc.result.GraceJSONResult;
import com.imooc.utils.OSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author liansen
 * @create 03-20-15:07
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/uploadFace1")
    public GraceJSONResult uploadFace1(@RequestParam("file") MultipartFile file,
                                      @RequestParam("userId") String userId,
                                      HttpServletRequest request) throws IOException {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名 - 文件原始名可能是a.b.c.png; 获取到.png
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建文件新名称
        String newName = userId + suffixName;
        //指定文件存放位置
        String url = "D:\\temp\\face\\" + newName;
        //上传文件
        File newFile = new File(url);
        //判断目录是否存在
        if(!newFile.getParentFile().exists()){
            //创建目录
            newFile.getParentFile().mkdirs();
        }
        //上传
        file.transferTo(newFile);
        return GraceJSONResult.ok();
    }

    /**
     * 上传app端用户头像
     * @param file
     * @param userId
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFace")
    public GraceJSONResult uploadFace(@RequestParam("file") MultipartFile file,
                                      @RequestParam("userId") String userId) throws Exception {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名 - 文件原始名可能是a.b.c.png; 获取到.png
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建文件新名称
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = userId + "/" + fileName;
        String url = OSSUtils.uploadFile(file, path);
        log.info("url: " + url);
        return GraceJSONResult.ok(url);
    }


    /**
     * base64上传管理端用户头像
     * @param base64FileBO
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadAdminFace")
    public GraceJSONResult uploadAdminFace(@RequestBody Base64FileBO base64FileBO) throws Exception {
        String base64 = base64FileBO.getBase64File();
        String suffixName = ".png";
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = "base64" + "/" + fileName;

        // 判断是否base64是否包含data:image/png;base64等前缀，如果有则去除
        if (base64.contains("data:image/png;base64")) {
            base64 = base64.substring(22);
            System.out.println("包含png"+base64);
        }
        if (base64.contains("data:image/jpeg;base64")) {
            base64 = base64.substring(23);
            System.out.println("包含jpeg"+base64);
        }
        if (base64.contains("data:application/pdf;base64")) {
            base64 = base64.substring(28);
            System.out.println("包含pdf"+base64);
        }
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] bytes = base64Decoder.decodeBuffer(base64);
        String base64Url = OSSUtils.uploadBase64File(path, bytes);
        return GraceJSONResult.ok(base64Url);
    }

    /**
     * 上传公司logo
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadLogo")
    public GraceJSONResult uploadLogo(@RequestParam("file") MultipartFile file) throws Exception {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名 - 文件原始名可能是a.b.c.png; 获取到.png
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建文件新名称
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = "company/logo" + "/" + fileName;
        String url = OSSUtils.uploadFile(file, path);
        log.info("url: " + url);
        return GraceJSONResult.ok(url);
    }

    /**
     * 上传营业执照
     */
    @PostMapping("/uploadBizLicense")
    public GraceJSONResult uploadBizLicense(@RequestParam("file") MultipartFile file) throws Exception {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名 - 文件原始名可能是a.b.c.png; 获取到.png
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建文件新名称
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = "company/bizlicense" + "/" + fileName;
        String url = OSSUtils.uploadFile(file, path);
        log.info("url: " + url);
        return GraceJSONResult.ok(url);
    }

    /**
     * 上传企业授权书
     */
    @PostMapping("/uploadAuthLetter")
    public GraceJSONResult uploadAuthLetter(@RequestParam("file") MultipartFile file) throws Exception {
        //获取文件原始名称
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀名 - 文件原始名可能是a.b.c.png; 获取到.png
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //创建文件新名称
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = "company/AuthLetter" + "/" + fileName;
        String url = OSSUtils.uploadFile(file, path);
        return GraceJSONResult.ok(url);
    }

    /**
     * 上传企业相册
     */
    @PostMapping("/uploadPhoto")
    public GraceJSONResult uploadPhoto(@RequestParam("files") MultipartFile[] files,
                                       @RequestParam("companyId") String companyId) throws Exception {

        List<String> paths = new ArrayList<>();

        for(MultipartFile f : files){
            String originalFilename = f.getOriginalFilename();
            String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String fileName = uuid + suffixName;
            String path = "company/" + companyId +  "/photo" + "/" + fileName;
            String url = OSSUtils.uploadFile(f, path);
            paths.add(url);
        }

        return GraceJSONResult.ok(paths);
    }

    //****************************文章模块文件操作**********************************

    @PostMapping("/uploadArticleCover")
    public GraceJSONResult uploadArticleCover(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = "article/cover/" + fileName;
        String url = OSSUtils.uploadFile(file, path);
        return GraceJSONResult.ok(url);

    }


    //聊天的时候发图片，会先将图片上传，然后前端拿到url去显示即可
    @PostMapping("uploadChatPhoto")
    public GraceJSONResult uploadChatPhoto(@RequestParam("file") MultipartFile file, String userId) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = uuid + suffixName;
        String path = "chat/" + userId + "/" + fileName;
        String url = OSSUtils.uploadFile(file, path);
        return GraceJSONResult.ok(url);
    }

}
